﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DevCore.DB;
using DevCore.Entity.System;
using DevCore.Refection;
using DevCore.Service.Db;
using DevCore.UI.Web.Grid;
using DevCore.Utility;

namespace DevCore.Service.System
{
    /// <summary>
    /// 功能服务类
    /// </summary>
    public class FunctionService
    {
        /// <summary>
        /// 得到功能基本信息
        /// </summary>
        /// <param name="funcCode">功能编码</param>
        /// <returns></returns>
        public static FunctionInfo GetEntity(String funcCode)
        {
            DataTable dtInfo = DbDataService.GetRowsBySql("App_S_Function", "*", "FuncCode='" + funcCode + "'", "");
            FunctionInfo entity = new FunctionInfo();
            RefectionHelper.SetEntityInfo<FunctionInfo>(entity, dtInfo);
            return entity;
        }

        /// <summary>
        /// 得到当前功能列的集合
        /// </summary>
        /// <param name="funcCode">功能编码</param>
        /// <returns></returns>
        public static List<GridColumnInfo> GetColumns(String funcCode)
        {
            bool isTrans = false;
            List<GridColumnInfo> gridColumnInfos = new List<GridColumnInfo>();
            List<DbParamInfo> dicParams = new List<DbParamInfo>();
            dicParams.Add(new DbParamInfo(){Name = "FuncCode", DbType = "VarChar", Direction = 0, Size = 50, Value = funcCode});
            DataTable dtInfo = DbDataService.ExecuteDataTableProc("App_Sp_sGetColumnsByFuncCode", dicParams);
            if(dtInfo != null && dtInfo.Rows.Count > 0)
            {
                foreach (DataRow drInfo in dtInfo.Rows)
                {
                    GridColumnInfo gridColumnInfo = new GridColumnInfo();
                    gridColumnInfo.FieldName = StringHelper.ObjectToStr(drInfo["FieldName"]);
                    gridColumnInfo.FieldCaption = StringHelper.ObjectToStr(drInfo["FieldCaption"]);
                    gridColumnInfo.IsCheckBox = StringHelper.ObjectParse<bool>(drInfo["IsCheckBox"], out isTrans);
                    gridColumnInfo.Width = StringHelper.ObjectToStr(drInfo["Width"]);
                    gridColumnInfos.Add(gridColumnInfo);
                }
            }

            return gridColumnInfos;
        }

        /// <summary>
        /// 得到这个功能对应数据表
        /// </summary>
        /// <param name="dealAction">=1 表示不采用通用处理。采用特殊处理方式。</param>
        /// <param name="funcCode">功能编码</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderBy"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public static DataTable GetFuncDataTable(int dealAction,String funcCode, int pageIndex, int pageSize, String strWhere, String orderBy, out int recordCount)
        {
            int retRecordCount = 0;
            List<DbParamInfo> dicParams = new List<DbParamInfo>();
            dicParams.Add(new DbParamInfo() { Name = "DealAction", DbType = "Int", Direction = 0, Size = 50, Value = dealAction });
            dicParams.Add(new DbParamInfo() { Name = "FuncCode", DbType = "VarChar", Direction = 0, Size = 50, Value = funcCode });
            dicParams.Add(new DbParamInfo() { Name = "WhereParams", DbType = "VarChar", Direction = 0, Size = 50, Value = strWhere });
            dicParams.Add(new DbParamInfo() { Name = "OrderBy", DbType = "VarChar", Direction = 0, Size = 50, Value = orderBy });
            dicParams.Add(new DbParamInfo() { Name = "PageIndex", DbType = "VarChar", Direction = 0, Size = 50, Value = pageIndex });
            dicParams.Add(new DbParamInfo() { Name = "PageSize", DbType = "VarChar", Direction = 0, Size = 50, Value = pageSize });
            dicParams.Add(new DbParamInfo() { Name = "RecordCount", DbType = "BigInt", Direction = 1, Size = 50, Value = 1 });
            DataTable dtInfo = DbDataService.ExecuteDataTableProc("App_Sp_sGetDataTableByFuncCode", dicParams);

            bool isTran = false;
            retRecordCount = StringHelper.ObjectParse<int>(GetDbParamValue("RecordCount", dicParams),0, out isTran);
            recordCount = retRecordCount;
            return dtInfo;
        }

        /// <summary>
        /// 得到一个参数名的具体值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dicParams"></param>
        /// <returns></returns>
        private static object GetDbParamValue(String name, List<DbParamInfo> dicParams)
        {
            foreach (DbParamInfo dbParamInfo in dicParams)
            {
                if(dbParamInfo.Name.ToLower().Equals(name.ToLower()))
                {
                    return dbParamInfo.Value;
                    break;
                }
            }
            return null;
        }

        

        
    }
}
